﻿using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;

namespace ECBS_Backend.utils
{
    public static class JwtGenerator
    {

        private const string SecretKey = "asdfghjlojykhmcmkiwdljwaafaaefsgsegofhdwlahfagfwaf";

        /// <summary>
        /// 生成token
        /// </summary>
        /// <returns></returns>
        public static string GenerateJWT(string role, string user)
        {
            var algorithm = SecurityAlgorithms.HmacSha256;
            var claims = new[]
            {
                new Claim(ClaimTypes.Name, user),
                new Claim(ClaimTypes.Role, role),
            };

            var secretKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(SecretKey));
            var signingCredentials = new SigningCredentials(secretKey, algorithm);

            var token = new JwtSecurityToken(
                "Issuer",    //Issuer
                "Audience",  //Audience
                claims,                          //Claims,
                DateTime.Now,                    //notBefore
                DateTime.Now.AddDays(1),         //expires
                signingCredentials
            );
            var jwtToken = new JwtSecurityTokenHandler().WriteToken(token);

            return jwtToken;
        }
    }
}
